package apy.codecase.business.presenter;

import android.content.Context;

import java.util.List;

import apy.codecase.App;
import apy.codecase.base.BasePresenterImpl;
import apy.codecase.base.BaseView;
import apy.codecase.greendao.DiaryDaoUtils;
import apy.codecase.model.DiaryDaoModel;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 *
 * @author Administrator
 * @date 2017/10/30
 */

public class DiaryImpl extends BasePresenterImpl {

    public DiaryImpl(Context mContext, BaseView<List<DiaryDaoModel>, DiaryDaoModel> baseView) {
        super(mContext, baseView);
    }

    @Override
    public void loadData(Object... objects) {
        final DiaryDaoUtils utils = (DiaryDaoUtils) objects[0];
        Flowable.just(utils)
                .subscribeOn(Schedulers.newThread())
                .map(new Function<DiaryDaoUtils, List<DiaryDaoModel>>() {
                    @Override
                    public List<DiaryDaoModel> apply(@NonNull DiaryDaoUtils diaryDaoUtils) throws Exception {
                        return diaryDaoUtils.queryDiaryDaoModelByQueryBuilderID(App.TICKET);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<DiaryDaoModel>>() {
                    @Override
                    public void accept(List<DiaryDaoModel> diaryDaoModels) throws Exception {
                        baseView.afterLoadData(diaryDaoModels);
                    }
                });
    }
}
